package com.xiong.xtools.mainstart;

import android.Manifest;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.Priority;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.xiong.xtools.R;
import com.xiong.xtools.XToast.ToastActivity;
import com.xiong.xtools.citychoose.CityChooseActivity;
import com.xiong.xtools.coordinatortablayout.CoorActivity;
import com.xiong.xtools.countdown.CountDownActivity;
import com.xiong.xtools.dialog.AlertDialogActivity;
import com.xiong.xtools.draw.DooleViewActivity;
import com.xiong.xtools.gesturelock.GestureLockActivity;
import com.xiong.xtools.like.ActivityLike;
import com.xiong.xtools.myguide.GuideActivity;
import com.xiong.xtools.photo.PhotoActivity;
import com.xiong.xtools.progressbar.PregressBarActivity;
import com.xiong.xtools.qrcode.QRCodeActivity;
import com.xiong.xtools.searchbox.SearchActivity;
import com.xiong.xtools.sectionrecyclerview.SectionRecyclerActivity;
import com.xiong.xtools.slip.SlipActivity;
import com.xiong.xtools.springIndicator.SpringIndicatorActivity;
import com.xiong.xtools.switchbutton.SwitchButtonActivity;
import com.xiong.xtools.twolist.TwoListViewActivity;
import com.xiong.xtools.twolistdown.TwoListDownActivity;

import java.util.ArrayList;
import java.util.List;
/**
 * Author : 熊文强
 * Time : 2017/9/19
 * Content : 网络上收集的各种高级功能及工具类
 */
public class MainActivity extends AppCompatActivity {

	//双击返回键 退出
	private static final int TIME_INTERVAL = 2000;
	GridView gridView;
	private List<MainItem> mData;
	private MainActivity mContext;
	private long mBackPressed;
	private MyAdapter adapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		mContext = this;
		initData();
		initView();

		// 动态权限申请
		XiongPermissionUtil.
				with(mContext).
				addPermission(Manifest.permission.ACCESS_FINE_LOCATION).
				addPermission(Manifest.permission.ACCESS_COARSE_LOCATION).
				addPermission(Manifest.permission.READ_EXTERNAL_STORAGE).
				addPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE).
				addPermission(Manifest.permission.CAMERA).
				addPermission(Manifest.permission.READ_PHONE_STATE).
				initPermission();
	}

	private void initData() {
		mData = new ArrayList<>();
		mData.add(new MainItem("二维码与条形码扫描", R.mipmap.ic_main_code, QRCodeActivity.class));
		mData.add(new MainItem("圆形头像工具类", R.mipmap.ic_main_head, PhotoActivity.class));
		mData.add(new MainItem("省市县三级联动地区选择", R.mipmap.ic_main_city, CityChooseActivity.class));

		mData.add(new MainItem("渐变颜色的引导页", R.mipmap.ic_main_color, GuideActivity.class));
		mData.add(new MainItem("Tab和Coordinator联合使用", R.mipmap.ic_main_tab, CoorActivity.class));
		mData.add(new MainItem("黏连效果滑动页", R.mipmap.ic_main_spring, SpringIndicatorActivity.class));

		mData.add(new MainItem("双ListView（左右）点餐界面", R.mipmap.ic_main_list, TwoListViewActivity.class));
		mData.add(new MainItem("双ListView（上下）资产界面", R.mipmap.ic_main_list2, TwoListDownActivity.class));
		mData.add(new MainItem("实现分组列表RecyclerView", R.mipmap.ic_main_section, SectionRecyclerActivity.class));

		mData.add(new MainItem("自定义画板", R.mipmap.ic_main_doole, DooleViewActivity.class));
		mData.add(new MainItem("仿QQ左滑置顶，删除", R.mipmap.ic_main_qq, SlipActivity.class));
		mData.add(new MainItem("动画搜索框", R.mipmap.ic_main_search, SearchActivity.class));

		mData.add(new MainItem("各种手势解锁", R.mipmap.ic_main_lock, GestureLockActivity.class));
		mData.add(new MainItem("数字进度条", R.mipmap.ic_main_pregress, PregressBarActivity.class));
		mData.add(new MainItem("各种类型Dialog", R.mipmap.ic_main_dialog, AlertDialogActivity.class));
		mData.add(new MainItem("各种选择开关按钮", R.mipmap.ic_main_switch, SwitchButtonActivity.class));
		mData.add(new MainItem("各种点赞控件", R.mipmap.ic_main_like, ActivityLike.class));
		mData.add(new MainItem("各类Toast", R.mipmap.ic_main_toast, ToastActivity.class));

		mData.add(new MainItem("闹钟倒计时", R.mipmap.ic_main_countdown, CountDownActivity.class));
	}

	// 设置GridView属性
	private void initView() {
		gridView = (GridView) findViewById(R.id.gridView);
		gridView.setNumColumns(3);
		gridView.setHorizontalSpacing(20);
		gridView.setVerticalSpacing(20);
		adapter = new MyAdapter(mContext);
		gridView.setAdapter(adapter);
	}

	@Override
	public void onBackPressed() {
		if (mBackPressed + TIME_INTERVAL > System.currentTimeMillis()) {
			super.onBackPressed();
			return;
		} else {
			Toast.makeText(getBaseContext(), "再次点击返回键退出", Toast.LENGTH_SHORT).show();
		}
		mBackPressed = System.currentTimeMillis();
	}

	// 自定义适配器
	private class MyAdapter extends BaseAdapter {
		LayoutInflater inflater;
		Context context;

		public MyAdapter(Context context) {
			this.context = context;
			inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		}

		@Override
		public int getCount() {
			return mData.size();
		}

		@Override
		public Object getItem(int position) {
			return mData.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			final MainItem mainItem = mData.get(position);
			ViewHolder holder = null;
			if (convertView == null) {
				convertView = inflater.inflate(R.layout.item_recyclerview_main, null);
				holder = new ViewHolder();
				holder.myView = (LinearLayout) convertView.findViewById(R.id.myView);
				holder.imageView = (ImageView) convertView.findViewById(R.id.imageView);
				holder.tvName = (TextView) convertView.findViewById(R.id.tv_name);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}

			Glide.with(context).
					load(mainItem.getImage()).
					diskCacheStrategy(DiskCacheStrategy.RESULT).
					thumbnail(0.5f).
					priority(Priority.HIGH).
					placeholder(R.mipmap.ic_launcher).
					error(R.mipmap.ic_launcher).
					fallback(R.mipmap.ic_launcher).
					into(holder.imageView);

			holder.tvName.setText(mainItem.getName());

			holder.myView.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View view) {
					XActivityUtils.skipActivity(context, mainItem.getActivity());
				}
			});
			return convertView;
		}
	}

	static class ViewHolder {
		ImageView imageView;
		TextView tvName;
		LinearLayout myView;
	}
}
